package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TourismPact;
import com.ruoyi.system.domain.TourismUser;
import com.ruoyi.system.mapper.TourismPactMapper;
import com.ruoyi.system.mapper.TourismUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TourismFoodMapper;
import com.ruoyi.system.domain.TourismFood;
import com.ruoyi.system.service.ITourismFoodService;

import javax.annotation.Resource;

/**
 * 美食Service业务层处理
 *
 * @author gqd
 * @date 2020-12-25
 */
@Service
public class TourismFoodServiceImpl implements ITourismFoodService
{
    @Autowired
    private TourismFoodMapper tourismFoodMapper;
    @Resource
    private TourismPactMapper tourismPactMapper;
    @Resource
    private TourismUserMapper tourismUserMapper;

    /**
     * 查询美食
     *
     * @param id 美食ID
     * @return 美食
     */
    @Override
    public TourismFood selectTourismFoodById(Integer id)
    {
        return tourismFoodMapper.selectTourismFoodById(id);
    }

    /**
     * 查询美食列表
     *
     * @param tourismFood 美食
     * @return 美食
     */
    @Override
    public List<TourismFood> selectTourismFoodList(TourismFood tourismFood)
    {
        return tourismFoodMapper.selectTourismFoodList(tourismFood);
    }

    /**
     * 新增美食
     *
     * @param tourismFood 美食
     * @return 结果
     */
    @Override
    public int insertTourismFood(TourismFood tourismFood)
    {
        tourismFood.setCreateTime(DateUtils.getNowDate());
        return tourismFoodMapper.insertTourismFood(tourismFood);
    }

    /**
     * 修改美食
     *
     * @param tourismFood 美食
     * @return 结果
     */
    @Override
    public int updateTourismFood(TourismFood tourismFood)
    {
        tourismFood.setUpdateTime(DateUtils.getNowDate());
        return tourismFoodMapper.updateTourismFood(tourismFood);
    }

    /**
     * 批量删除美食
     *
     * @param ids 需要删除的美食ID
     * @return 结果
     */
    @Override
    public int deleteTourismFoodByIds(Integer[] ids)
    {
        return tourismFoodMapper.deleteTourismFoodByIds(ids);
    }

    /**
     * 删除美食信息
     *
     * @param id 美食ID
     * @return 结果
     */
    @Override
    public int deleteTourismFoodById(Integer id)
    {
        return tourismFoodMapper.deleteTourismFoodById(id);
    }

    /**
     * 根据美食名称模糊查询所有的美食
     * @param
     * @return
     */
    @Override
    public AjaxResult selectFoodByFood(String foodName) {
        List<TourismFood> tourismFoods = tourismFoodMapper.selectFoodByFood(foodName);
        if(tourismFoods.size() == 0 || tourismFoods == null){
            return AjaxResult.error("暂时没有该美食的信息！");
        }
        return AjaxResult.success(tourismFoods);
    }

    /**
     * 根据美食id查询美食的相关的信息
     * @param foodId
     * @return
     */
    @Override
    public AjaxResult selectFoodAndRestaurant(Integer foodId,Integer userId) {
        TourismUser tourismUser = tourismUserMapper.selectByUserId(userId);

        TourismFood tourismFood = tourismFoodMapper.selectFoodAndRestaurant(foodId);
        if(tourismFood == null){
            return AjaxResult.error("暂时没有该美食的相关的信息！");
        }
        if(tourismUser == null || userId == null){
            tourismFood.setFlag("0");
        }else {
            TourismPact tourismPact = tourismPactMapper.selectSpotPact3(foodId,userId);
            if(tourismPact == null){
                tourismFood.setFlag("0");
            }else {
                tourismFood.setFlag("1");
            }
        }
        return AjaxResult.success(tourismFood);
    }

    /**
     * 查询特色美食
     * @param foodName
     * @return
     */
    @Override
    public AjaxResult selectFoodByStatus(String foodName) {
        List<TourismFood> tourismFoods = tourismFoodMapper.selectFoodByStatus(foodName);
        if(tourismFoods == null){
            return AjaxResult.error("暂时没有特色美食的信息！");
        }
        return AjaxResult.success(tourismFoods);
    }
}
